Public Class OrderView_Print
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblOrderNoText As System.Web.UI.WebControls.Label
    Protected WithEvents lblDepartureDate As System.Web.UI.WebControls.Label
    Protected WithEvents lblContainerNumber As System.Web.UI.WebControls.Label
    Protected WithEvents lblDestination As System.Web.UI.WebControls.Label
    Protected WithEvents lblComments As System.Web.UI.WebControls.Label
    Protected WithEvents lblFreightForwarder As System.Web.UI.WebControls.Label
    Protected WithEvents trViewOrder As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents tbEditForm As System.Web.UI.HtmlControls.HtmlTable
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

#Region "   Properties"
    Protected Property PageBO() As COrder
        Get
            Return CType(Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME), COrder)
        End Get
        Set(ByVal Value As COrder)
            Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME) = Value
        End Set
    End Property

    Protected Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

    Private ReadOnly Property ReturnURL() As String
        Get
            If IsNothing(Request.QueryString("ru")) Then
                Return Me.UrlReferrer
            End If
            Return Server.UrlDecode(Request.QueryString("ru"))
        End Get
    End Property

    Protected ReadOnly Property OrderCustomer() As CUser
        Get
            Dim customer As New CUser
            customer.Load(PageBO.CustomerID)

            Return customer
        End Get
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then

            If CType(PageBO.OrderType, EOrderType) = EOrderType.Bulk_Order Then
                Me.lblOrderNoText.Text = "BO Number"

                ' hide From BO# column
                Me.DataGrid1.Columns(Me.DataGrid1.Columns.Count - 1).Visible = False
            Else
                Me.lblOrderNoText.Text = "PO Number"
            End If

            'Display the order's complete information if any.
            DisplayCompleteInformation()

            'Display all product items of this order.
            Me.DataGrid1.DataSource = PageBO.OrderItems
            Me.DataGrid1.DataBind()

        End If
    End Sub

    Private Sub DisplayCompleteInformation()

        If (PageBO.CompleteInfo) Then
            Me.lblDepartureDate.Text = MIMS_WRS.Business.Common.FormatDate(PageBO.DepartureDate)
            Me.lblDestination.Text = PageBO.Destination
            Me.lblFreightForwarder.Text = GetFreightForwarder(PageBO.FreightID)
            Me.lblContainerNumber.Text = PageBO.ContainerNumber
            Me.lblComments.Text = PageBO.Comments

            Me.tbEditForm.Visible = True
        End If
    End Sub

    Protected Function GetContactName() As String
        If PageBO.CustomerID > 0 AndAlso PageBO.OrderByCustomerID > 0 Then
            Dim objCusContact As New CCustomerContact
            Try
                objCusContact.Load(PageBO.OrderByCustomerID)
            Catch ex As Exception
                Throw New ApplicationException("Can not load customer name", ex)
            End Try

            'check for data integrity
            If objCusContact.CustomerID <> PageBO.CustomerID Then
                Throw New ApplicationException("Order data invalid, please check for customer id and customer's contact id")
            End If

            Return objCusContact.ContactName
        Else
            Return Nothing
        End If
    End Function

    Private Function GetFreightForwarder(ByVal freightID As Integer) As String
        Dim obj As New CLookupData
        obj.Load(freightID)

        Return obj.Value1
    End Function

    Protected Function GetRequestedETAText() As String
        Select Case CType(PageBO.RequestedETA, ERequestedETAType)
            Case ERequestedETAType.As_soon_as_possible
                Return CType(PageBO.RequestedETA, ERequestedETAType).ToString.Replace("_", " ")
            Case ERequestedETAType.After, ERequestedETAType.Before
                Return String.Format("{0} {1}", CType(PageBO.RequestedETA, ERequestedETAType).ToString, FormatDate(PageBO.ETADate1))
            Case ERequestedETAType.Between
                Return String.Format("Between {0} and {1}", FormatDate(PageBO.ETADate1), FormatDate(PageBO.ETADate2))
        End Select
    End Function

    Protected Function GetShippingTypeText() As String
        Return CType(PageBO.ShippingType, EShippingType).ToString.Replace("_", " ")
    End Function

    Protected Function GetPackingTypeText() As String
        Return CType(PageBO.PackingType, EPackingType).ToString.Replace("_", " ")
    End Function

    Protected Function GetContainerText() As String
        Return String.Format("{0} x {1}", PageBO.Containers, CType(PageBO.ContainerType, EContainerType).ToString.Replace("_", " "))
    End Function

    Protected Function GetAdjustedQuantityText(ByVal container As Object) As String
        If CBool(DataBinder.Eval(container, "DataItem.IsQuantityAdjusted")) = True Then
            Return CStr(DataBinder.Eval(container, "DataItem.AdjustedQuantity"))
        Else
            Return "N/A"
        End If
    End Function

    Protected Function GetEditIndex(ByVal container As Object) As String
        Dim dgi As DataGridItem = CType(container, DataGridItem)
        Return String.Format("if (!btnEdit_onclick({0})) return false;", dgi.ItemIndex)
    End Function

End Class
